Page({
  data: {
    selectflag:false,
    isShowRules:false,
    isShowVideo:false,
		hang:5,

		mineInfo:{},

		taskStutas:[
			{
				type:'member',
				end:false,
				aLin:'MEMBER'
			},
			{
				type:'follow',
				end:false,
				aLin:'FOLLOW'
			},
			{
				type:'colle',
				end:false,
				aLin:'COLLECT'
			},
			{
				type:'showShare',
				end:false,
				aLin:'none'
			},
			{
				type:'seeVideo',
				end:false,
				aLin:'VIDEO'
			},
			{
				type:'seePage',
				end:false,
				aLin:'VIEW'
			}
		],

		current_energy:0, //用户当前能量

		isShowTaskEnd:false, //是否显示任务已完成
		isShowRes:false,     //是否显示任务完成弹窗
		isShowShare:false,	 //是否显示分享弹窗

		openId:0,


	},

	//点击去完成任务
	clickTask(e){

		if(this.data.taskStutas[e.target.dataset.index].end){

			this.setData({
				isShowTaskEnd:true
			})

			return
		}

		this[e.target.dataset.type]()

		

		console.info(e.target.dataset.type)
	},

	//跳转页面
	goPage(e){
		my.navigateTo({
			url: e.target.dataset.url
		});
	},
	

	showRules(){
		this.setData({
			isShowRules:true
		})
	},

	//继续游戏
	againGame(){
		//开始游戏前判断剩余游戏次数是否充足

		my.showLoading();

		getApp().cloud.function.invoke('client',{},"getCurrentUserInfo").then(res => {
			console.log('获取剩余游戏次数=====>',res); 

			my.hideLoading();

			if(res.data.game_left_num <= 0){
				my.navigateTo({
					url: '../drawResult/drawResult?end=gameNo'
				});
			}else{
				my.navigateTo({
					url: '../game/game'
				});
			}
		})
		
		
	
	},
	
	//入会
  member(){
		
    my.call('navigateToOutside',{    
			
			//正式
   		url:'https://market.m.taobao.com/app/sj/shop-membership-center/pages/index?wh_weex=true&sellerId=2794371653&extraInfo=%7b%22source%22%3a%22CHAMELEON%22%7d&callbackUrl=https%3A%2F%2Fm.duanqu.com%3F_ariver_appid%3D3000000035452835%26page%3Dpages%2Findex%2Findex'
			 
			//测试
			//  url:'https://market.m.taobao.com/app/sj/shop-membership-center/pages/index?wh_weex=true&sellerId=2794371653&extraInfo=%7b%22source%22%3a%22CHAMELEON%22%7d&callbackUrl=https%3a%2f%2fm.duanqu.com%3f_ariver_appid%3d3000000035452835%26nbsv%3d0.0.1%26nbsource%3ddebug%26nbsn%3dTRIAL%26_mp_code%3dtb%26page%3dpages%2findex%2findex'
		},()=>{
			// let body = 
			setTimeout(() => {
				getApp().cloud.function.invoke('client',{nick_name:this.data.mineInfo.nickName},"member").then(res => {
					console.log('当前用户入会===》',res,this.data.mineInfo.nickName);
					if(res.code != 0){
						my.showToast({
							content:res.msg
						});

						return
					}

					this.setData({
						isShowRes:true
					})

					this.reqTask()

				}) 
			}, 3000);

			
		})
	},

	//关注店铺
	follow(){
		my.tb.checkShopFavoredStatus({
				id: 2794371653,
				success: (res) => {
					console.info(res)

					if(res.isFavor){
						this.follwReq()
					}else{
						my.tb.favorShop({
							id: 2794371653,
							success: (res) => {
								this.follwReq()
							}
						})
					}

				},
				fail: (res) => {
					console.info(res)
				}
		})
	},	

	//关注店铺的接口
	follwReq(){
		getApp().cloud.function.invoke('client',{},"follow").then(res => {
			console.info('关注店铺=====>',res)

			if(res.code != 0){

				if(res.code == -1){
					this.setData({
						isShowTaskEnd:true
					})

					return
				}

				my.showToast({
					content:res.msg,
					duration:3000
				});

				return
			}

			this.setData({
				isShowRes:true
			})

			this.reqTask()
		})
	},

	//收藏好货
	colle(){
		my.navigateTo({
			url: '../collection/collection'
		});
	},

	//浏览页面
	seePage(){
		my.call('navigateToOutside',{          
   		url:'https://swisse.m.tmall.com/?ajson=1&parentCatId=0&refer=https%3A%2F%2Fkb-render.alicdn.com%2F&spm=687.8433302.20001.1.B17eWH&mytmenu=mdianpu&utkn=g%2Con3ws43tmw45tn55xkr43ywg5s62fnpk1537174238908&user_number_id=2794371653&scm=1028.1.1.20001&ali_trackid=41_9c5d45fa94a0ce85282a8b1ac013461f'
		},(res)=>{
			
			setTimeout(() => {
				getApp().cloud.function.invoke('client',{nick_nam:this.data.mineInfo.nickName},"viewPage").then(res => {
					console.info('关注店铺=====>',res)
					
					if(res.code != 0){
						my.showToast({
							content:res.msg
						});

						return
					}

					this.setData({
						isShowRes:true
					})

					this.reqTask()
					
				})
			}, 3000);
			
		})
	},

	//请求能量值
	reqEnergy(){
		getApp().cloud.function.invoke('client',{},"getCurrentUserInfo").then(res => {
			console.log('当前用户信息===》',res); 

			if(res.code != 0){
				my.alert({
					content: res.msg
				})

				return;
			}

			this.setData({
				current_energy:res.data.current_energy,
				openId:res.data.open_id
			})



		})  
	},


	//获取任务详情
	reqTask(){
		getApp().cloud.function.invoke('client',{},"taskStatus").then(res => {
			console.info('获取任务进度==>',res)

			if(res.code != 0){
				my.showToast({
					content:res.msg
				});
				return
			}
			
			let aArr = [
					{
						type:'member',
						end:false,
						aLin:'MEMBER'
					},
					{
						type:'follow',
						end:false,
						aLin:'FOLLOW'
					},
					{
						type:'colle',
						end:false,
						aLin:'COLLECT'
					},
					{
						type:'showShare',
						end:false,
						aLin:'none'
					},
					{
						type:'seeVideo',
						end:false,
						aLin:'VIDEO'
					},
					{
						type:'seePage',
						end:false,
						aLin:'VIEW'
					}
				]
			

			aArr.map((v,i) => {
				res.data.map((x,y) => {
					if(v.aLin == x.type){
						v.end = true
					}
				})
			})

			this.setData({
				taskStutas:aArr
			})
		})
	},

	//弹出分享弹窗
	showShare(){
		this.setData({
			isShowShare:true
		})
	},

	//点击去看视频
	seeVideo(){
		my.navigateTo({
			url: '../video/video'
		});
	},

	onShow(){
		this.reqEnergy()
		this.reqTask()
		// this.checkVideoEnd()
	},

	//检查视频是否观看完毕
	checkVideoEnd(){
		getApp().cloud.function.invoke('client',{},"totalVideoStatus").then(res => {
			console.info('获取观看视频进度任务进度==>',res)

			if(res.code != 0){
				my.showToast({
					content:res.msg
				});
				return
			}
			
			let lin = this.data.taskStutas


			if(res.data){

				lin.map((v,i) => {
					if(v.aLin == 'VIDEO'){
						v.end = true
					}
				})


				this.setData({
					taskStutas
				})
			}


		})
	},
	
	onLoad() {
		my.authorize({
			scopes: 'scope.userInfo',
			success: () => {
				my.getAuthUserInfo({
					success: (userInfo) => {
						this.setData({
							mineInfo:userInfo
						})

						// console.info(userInfo.avatar)
						
					}
				});
			},
		});
	},
	
	onShareAppMessage() {
    return {
      title: 'swisse官方海外旗舰店',
			desc: '【快来和我一起参与！swisse斯维诗海外旗舰店玩游戏集能量，赢IPhone12 Pro】我已经参与了，就等你啦！',
			imgUrl:'https://img.alicdn.com/imgextra/i4/2794371653/O1CN01rSGpJH1O57MGYdPqj_!!2794371653.jpg',
      // path: `pages/index/index?nickName=${this.data.mineInfo.nickName}&avater=${encodeURI(this.data.mineInfo.avatar)}`  
      path: `pages/index/index?nickName=${this.data.mineInfo.nickName}&avater=${this.data.mineInfo.avatar}&openId=${this.data.openId}`  
		};  
	}
});
